"use strict";

/**
 * @ngdoc function
 * @name jgappApp.controller:ProjectProjectbxCtrl
 * @description
 * # ProjectProjectbxCtrl
 * Controller of the jgappApp
 */
angular
  .module("jgappApp")
  .controller("ProjectProjectbxCtrl", function (
    $scope,
    $rootScope,
    $location,
    openApi,
    Util,
    $routeParams,
    FormSubmit,
    localStorageService
  ) {
    var id = ($scope.process_id = $routeParams.id);
    // 选择材料类型
    $scope.getProjectBXTypes = function () {
      var param = {};
      param.type = "项目报销类型";
      Util.getKeyValue(param, function (data) {
        $scope.projectBXTypes = data.data;
      });
    };
    $scope.chooseProject = function () {
      // 将列表的数据序列化存入缓存以便回来时候使用
      Util.savaParamForReview($scope.param, id);
      $location.path("project/chooseproject");
    };

    // 选择项目收款
    $scope.chooseSk = function () {
      var params = $scope.param;
      if (!params.projectId) {
        Util.Toast("请先选择项目");
        return;
      }
      var projectParams = {
        projectId: params.projectId,
        formKey: "Xm_ProjectGathering",
        fieldName: "gatheringNo"
      };
      // 清掉关联的编号
      Util.savaParamForReview(params, id);
      localStorageService.set("projectParams", projectParams);
      $location.path("/project/choosedata");
    };

    $scope.chooseSupplier = function () {
      var params = $scope.param;
      var projectParams = {
        formKey: "Xm_SupplierStore",
        fieldName: "supplierNo"
      };

      // 清掉关联的编号
      Util.savaParamForReview(params, id);
      localStorageService.set("projectParams", projectParams);
      $location.path("/project/choosedata");
    };

    $scope.showPicker = function (index) {
      $scope.showCerPicker = true;
      $scope.editIndex = index;
      $rootScope.$emit("showmasker");
    };
    $scope.edit = function (index) {
      if ($scope.curindex !== -1) {
        $scope.resetlist()
        return
      }
      var current = $scope.param.details[index];
      if (current.startTime) {
        current.startTime = current.startTime.split(" ")[0]
      }
      if (current.endTime) {
        current.endTime = current.endTime.split(" ")[0]
      }
      $scope.picker = Object.assign({}, current);
      $scope.showPicker(index)
    }
    $scope.deletedItem = function (index) {
      $scope.param.details.splice(index, 1);
      $scope.curindex = -1;
    };

    $scope.deletedBorrowItem = function (index) {
      $scope.param.borrows.splice(index, 1);
      $scope.payindex = -1;
    };

    $scope.addItem = function () {
      $scope.picker = $scope.picker || {};
      var check = false;
      check = checkPicker($scope.picker);
      if (!check) {
        return;
      }
      var data = Object.assign({}, $scope.picker);
      if (data.startTime) {
        data.startTime = data.startTime + " 00:00:00"
      }
      if (data.endTime) {
        data.endTime = data.endTime + " 00:00:00"
      }
      if ($scope.editIndex !== -1) {
        $scope.param.details.splice($scope.index, 1, data)
      } else {
        $scope.param.details.push(data);
      }
      // 隐藏表单
      closePicker();
    };
    var closePicker = function () {
      $scope.picker = {};
      $rootScope.$emit("closemasker");
      $scope.showCerPicker = false;
    };
    $scope.$on("closemasker", function () {
      closePicker();
    });
    $scope.closePicker = function () {
      closePicker();
    };
    $scope.swipe = function (index, type) {
      if (type) {
        $scope.payindex = index;
      } else {
        $scope.curindex = index;
      }
    };
    $scope.resetlist = function (type) {
      if (type) {
        $scope.payindex = -1;
      } else {
        $scope.curindex = -1;
      }
    };
    // 验证是否输入完整
    var checkPicker = function (data) {
      if (!data.category) {
        Util.Toast("请选择报销类型");
        return;
      }
      if (!data.amount) {
        Util.Toast("请输入报销金额");
        return;
      }
      if (!data.reason) {
        Util.Toast("请输入报销事由");
        return;
      }
      return true;
    };

    $scope.showSearchPicker = function () {
      $scope.searchtitle = "收款人";
      $scope.apiurl = openApi.projectreimburseHistory;
      $scope.showSearchInput = true;
      $rootScope.$emit("showmasker");
    };
    $scope.handleSearch = function (data) {
      $scope.param.receivePerson = data[0];
      $scope.param.receiveAccount = data[1];
      $scope.param.receiveBank = data[2];
      hideSearchPicker();
    };
    var hideSearchPicker = function () {
      $rootScope.$emit("closemasker");
      $scope.showSearchInput = false;
    };
    $scope.$on("closemasker", function () {
      hideSearchPicker();
    });

    $scope.chooselend = function () {
      if (!$scope.param.projectId) {
        Util.Toast("请先选择项目");
        return;
      }
      Util.savaParamForReview($scope.param, id);
      $location.path("/project/lendchoose/" + $scope.param.projectId);
    }
    var checkBorrowList = function (borrowingList) {
      if (borrowingList && borrowingList.length > 0) {
        for (let index = 0; index < borrowingList.length; index++) {
          const borrowItem = borrowingList[index];
          if (!borrowItem.reimburseAmount) {
            Util.Toast("请输入本次冲抵金额");
            return false
          }
        }
      }
      return true
    }
    $scope.submit = function (operationId) {
      // 底部类型end
      var options = {};
      // 流程id
      options.id = id;
      // 根据operationId判断是草稿还是提交
      options.operationId = operationId;
      options.urlParam = openApi.projectreimburse;
      options.param = angular.copy($scope.param, {});
      options.param.detailJson = JSON.stringify(options.param.details);
      options.param.borrowJson = JSON.stringify(options.param.borrows);

      delete options.param.details;
      delete options.param.borrows;
      FormSubmit.submit(options, $scope);
    };
    $scope.validate = function () {
      if (!$scope.param.projectName) {
        Util.Toast("请选择项目");
        return;
      }
      if ($scope.param.details.length < 1) {
        Util.Toast("请添加报销明细");
        return;
      }
      if (!checkBorrowList($scope.param.borrows)) {
        return;
      }

      if (!$scope.validation.passvalidation) {
        Util.Toast($scope.validation.tip);
        return;
      }
      return true;
    };
    var init = function () {
      // 获取回显的详细信息
      Util.getDetailData(id, function (data) {
        $scope.param = data;
        var data = localStorageService.get("projectinfo");
        localStorageService.remove("projectinfo");
        // 获取项目关联数据
        var projectreldata = localStorageService.get("projectreldata");
        localStorageService.remove("projectreldata");
        $scope.param = Object.assign({}, $scope.param, data, projectreldata);
        $scope.param.details = $scope.param.details || [];
      });
      $scope.getProjectBXTypes();
    };
    // 初始化
    init();
  });
